package com.gwm.auth.controller;

import com.gwm.auth.service.AuthService;
import com.gwm.common.auth.entity.UserInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController

public class AuthController {

    @Autowired
    private AuthService authService;
    @Autowired
    private HttpServletResponse response;
    @Autowired
    private HttpServletRequest request;

    /**
     * 登录授权
     *
     * @param username 用户名
     * @param password 密码
     * @return 无
     */
    @GetMapping("login")
    public ResponseEntity<Void> login(
            @RequestParam("username") String username,
            @RequestParam("password") String password,
            HttpServletResponse response) {
        // 登录
        authService.login(username, password, response);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }


    /**
     * 验证用户信息
     *
     * @return 用户信息
     */
    @ApiOperation(
            value = "校验用户名和密码",
            notes = "校验用户名和密码",
            httpMethod = "GET"
    )
    @GetMapping("verify")
    public ResponseEntity<UserInfo> verifyUser(HttpServletRequest request, HttpServletResponse response) {
        // 成功后直接返回
        return ResponseEntity.ok(authService.verifyUser(request, response));
    }



    /**
     * 退出登录
     */
    @PostMapping("logout")
    public ResponseEntity<Void> logout(HttpServletRequest request, HttpServletResponse response){
        authService.logout(request, response);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

}
